package com.msmall.community.tag;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.msmall.base.util.StringUtil;
import com.msmall.community.ApplicationManager;
import com.msmall.community.common.model.City;
import com.msmall.community.common.service.CityManager;

public class CityTag extends TagSupport{

	private static final long serialVersionUID = 7284894164855946166L;
	
	private String type; // text 显示地区文字：河北省/邯郸市/一号县； select select框格式的地区
	
	private String code;//区域码
	
	private boolean parent; //是否包含父区域 true :河北省/邯郸市/一号县; false:一号县； 只对 type=text有效
	
	private String onchange; // type=select时，onchange 事件
	
	private String pid;// type=select时，父选择框ID
	
	private String cid;// type=select时，子选择框ID
	
	private String pname;// type=select时，父选择框name
	
	private String cname;// type=select时，子选择框name
	
	public int doStartTag() throws JspException {
		return (SKIP_BODY);
	}

	public int doEndTag() throws JspException {
		JspWriter writer = pageContext.getOut();
		StringBuffer buf = new StringBuffer();
		try {
			CityManager manager = (CityManager) ApplicationManager.appContext.getBean("cityManager");
			City city = null;
			int level = 0;
			if(!StringUtil.nullOrBlank(code)){
				city = manager.getCityByCode(code);
				try{
					level = manager.getLevel(code);
				}catch(Exception e){
					level = 0;
				}
			}
			if("text".equalsIgnoreCase(type)){
				if(parent){
					City parentCity = manager.getUpCity(code);
					if(parentCity != null){
						City topCity = manager.getUpCity(parentCity.getCityCode());
						if(topCity != null){
							buf.append(topCity.getFullName()).append("/").append(parentCity.getFullName()).append("/").append(city.getFullName());
						}else{
							buf.append(parentCity.getFullName()).append("/").append(city.getFullName());
						}
					}else{
						buf.append(city.getFullName());
					}
				}else{
					buf.append(city.getFullName());
				}
			}
			if("select".equalsIgnoreCase(type)){
				List<City> top = manager.getTopCities();
				if(level == 1){
					buf.append("<select id=\"").append(pid).append("\" name=\"").append(pname).append("\" onchange=\"").append(onchange).append("\">");
					for(int i =0;i<top.size();i++){
						City c = top.get(i);
						if(code.equalsIgnoreCase(c.getCityCode())){
							buf.append(getOption(c.getCityCode(), c.getFullName(), true));
						}else{
							buf.append(getOption(c.getCityCode(), c.getFullName(), false));
						}
					}
					buf.append("</select>");
					List<City> children = manager.getChildCities(code);
					buf.append("<select id=\"").append(cid).append("\" name=\"").append(cname).append("\">");
					for(int i =0;i<children.size();i++){
						City c = children.get(i);
						buf.append(getOption(c.getCityCode(), c.getFullName(), false));
					}
					buf.append("</select>");
				}else if(level == 2 || (level == 3 && manager.isDirect(manager.getUpCode(manager.getUpCode(code))))){
					List<City> currents = manager.getSameParentCities(code);
					String parentCode = manager.getUpCode(code);
					buf.append("<select id=\"").append(pid).append("\" name=\"").append(pname).append("\" onchange=\"").append(onchange).append("\">");
					for(int i =0;i<top.size();i++){
						City c = top.get(i);
						if(parentCode.equalsIgnoreCase(c.getCityCode())){
							buf.append(getOption(c.getCityCode(), c.getFullName(), true));
						}else{
							buf.append(getOption(c.getCityCode(), c.getFullName(), false));
						}
					}
					buf.append("</select>");
					buf.append("<select id=\"").append(cid).append("\" name=\"").append(cname).append("\">");
					for(int i =0;i<currents.size();i++){
						City c = currents.get(i);
						if(code.equalsIgnoreCase(c.getCityCode())){
							buf.append(getOption(c.getCityCode(), c.getFullName(), true));
						}else{
							buf.append(getOption(c.getCityCode(), c.getFullName(), false));
						}
					}
					buf.append("</select>");
				}else if(level == 0){
					buf.append("<select id=\"").append(pid).append("\" name=\"").append(pname).append("\" onchange=\"").append(onchange).append("\">");
					for(int i =0;i<top.size();i++){
						City c = top.get(i);
						buf.append(getOption(c.getCityCode(), c.getFullName(), false));
					}
					buf.append("</select>");
					buf.append("<select id=\"").append(cid).append("\" name=\"").append(cname).append("\">")
					.append(getOption("", "--请选择--", false))
					.append("</select>");
				}
			}
		} catch (Exception e1) {
		}
		
		try {
			writer.print(buf.toString());
		} catch (IOException e) {
		}
		return (SKIP_BODY);
	}
	
	private String getOption(String value, String message, boolean selected){
		StringBuffer buf = new StringBuffer();
		buf.append("<option value=\"").append(value).append("\"");
		if(selected){
			buf.append("selected=\"selected\"");
		}
		buf.append(">").append(message).append("</option>");
		return buf.toString();
	}
	
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public boolean isParent() {
		return parent;
	}

	public void setParent(boolean parent) {
		this.parent = parent;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getOnchange() {
		return onchange;
	}

	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public String getCid() {
		return cid;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}

	public String getPname() {
		return pname;
	}

	public void setPname(String pname) {
		this.pname = pname;
	}

	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}
}
